package com.example.kafkalistener.Listener;

import com.alibaba.fastjson.JSON;
import com.example.kafkalistener.pojo.ResData;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.json4s.jackson.Json;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Liu
 * @date 2022/12/9 9:22
 **/

@Component
public class AllListener {

    List<JSON> list = new ArrayList<>() ;

    @KafkaListener(id = "kafkaConsumer", topics = "AllPeople", groupId = "niit01")
        public void listener1(ConsumerRecord<?, ?> record) {
        System.out.println(record.value());
        ResData.AllPeopleList = JSON.parseObject((String) record.value()) ;
    }

    @KafkaListener(id = "gradeConsumer",topics = "Grade", groupId = "niit01")
        public void listener2(ConsumerRecord<?, ?> record) {
        System.out.println(record.value());
        if (list.size() < 10 ){list.add(JSON.parseObject((String) record.value()));}
        ResData.GradeList = list;
    }

    @KafkaListener(id = "schoolConsumer", topics = "School", groupId = "niit01")
        public void listener3(ConsumerRecord<?, ?> record) {
        System.out.println(record.value());
        list.add(JSON.parseObject((String) record.value()));
        ResData.SchollList = list;
    }
}
